package idv.arthur.work;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ManipulateGridSubSys extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ManipulateGridSubSys() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = wid.getConn(true);
		PreparedStatement ps = null;
		ResultSet rs = null;
		String errPars = "}";
		String strSQL = "";
		String oper = request.getParameter("oper");
		String bss = request.getParameter("bssId");
		String id = "";
		String result = "";
		String currentUser =   request.getParameter("currentUser");
		
		try {
			/** 判別需進行何種操作 **/
			if ( "del".equalsIgnoreCase(oper) ) {
				id = request.getParameter("id");
				strSQL = "DELETE FROM subsys WHERE subsysid=? AND bssid=?";
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, id);
				ps.setString(2, bss);
				ps.execute();
				
				wid.keepLogProfile(wid.SUBSYS, wid.DELETE, currentUser, conn, ps);
				result = "刪除了"+id;
			} else if ( "add".equalsIgnoreCase(oper) ) {
				strSQL = "SELECT COUNT(*) FROM subsys WHERE subsysid=? AND bssid=?";
				id = request.getParameter("subSysId");
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, id);
				ps.setString(2, bss);
				rs = ps.executeQuery();
				rs.next();
				if ( rs.getInt(1) == 0 ) {
					strSQL = "INSERT INTO subsys (subsysid,subsysname,creator,createdDateTime,bssid) VALUES (?,?,?,?,?)";
					ps = conn.prepareStatement(strSQL);
					ps.setString(1, id);
					ps.setString(2, wid.UI2DB( request.getParameter("subSysName"),conn.getMetaData().getDatabaseProductName() ) ); 
					ps.setString(3, currentUser);
					ps.setTimestamp(4, wid.currentTimeStamp());
					ps.setString(5, bss);
					ps.execute();
					
					wid.keepLogProfile(wid.SUBSYS, wid.ADD, currentUser, conn, ps);
					result = "新增完成 "+id;
				} else {
					result = id+" 已存在 !";		
				}
			} else if ( "edit".equalsIgnoreCase(oper) ) {
				id = request.getParameter("id");
				strSQL = "UPDATE subsys SET subsysname=?,lastUpdator=?,lastUpdatedDateTime=? WHERE subsysid=? AND bssid=?";
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, wid.UI2DB( request.getParameter("subSysName"),conn.getMetaData().getDatabaseProductName() ) );
				ps.setString(2, currentUser);
				ps.setTimestamp(3, wid.currentTimeStamp());
				ps.setString(4, id);
				ps.setString(5, bss);
				ps.execute();	
				wid.keepLogProfile(wid.SUBSYS, wid.UPDATE, currentUser, conn, ps);
				result = "修改完成 "+id;
			}	
		} catch (SQLException e) {
			result = "異動"+id+"失敗!!\n\n";
			result += wid.errProcess(e);
			wid.kem(conn, ps, e, wid.toEType("SQLException")+errPars, wid.isPrint2Console );
		} catch (UnsupportedEncodingException e) {
			result = "異動"+id+"失敗!!\n\n";
			result += wid.errProcess(e);
			wid.kem(conn, ps, e, wid.toEType("UnsupportedEncodingException")+errPars, wid.isPrint2Console );
		} catch (Exception e) {
			result = "異動"+id+"失敗!!\n\n";
			result += wid.errProcess(e);
			wid.kem(conn, ps, e, wid.toEType("Exception")+errPars, wid.isPrint2Console );
		} finally {
			try {
				if ( rs!= null ) { rs.close(); }
				if ( ps!= null ) { ps.close(); }
				if ( conn!= null ) { conn.close(); }
			} catch (SQLException e) {
				System.out.println( wid.errProcess(e) );
			}	
		}
		response.getWriter().write(result);		
	}
}